<template>
  <div class="main-box-menu">
    <!-- 导航栏头部 logo -->
    <div class="main-box-menu-header">
      <img src="../../image/logo-header.png" alt="" />
    </div>

    <!-- 导航栏菜单 -->
    <div class="main-box-menu-list">
      <el-menu
        :default-active="active"
        class="el-menu-vertical-demo"
        :router="true"
        :unique-opened="true"
      >
        <el-menu-item index="/main/home">
          <el-icon
            ><img
              v-if="active !== '/main/home'"
              src="../../image/首页.svg"
              alt="" />
            <img
              v-else
              src="../../image/首页-2.svg"
              alt=""
          /></el-icon>
          <span>首页 Home Page</span>
        </el-menu-item>

        <el-sub-menu index="1">
          <template #title>
            <el-icon><OfficeBuilding /></el-icon>
            <span> 宿舍楼管理 Dormitory</span>
          </template>
          <el-menu-item
            v-for="(item, idx) in Dormitory"
            :key="idx"
            :index="item.path"
          >
            {{ item.name }}</el-menu-item
          >
        </el-sub-menu>

        <el-sub-menu index="2">
          <template #title>
            <el-icon><Operation /></el-icon>
            <span> 综合管理 MainManage</span>
          </template>
          <el-menu-item
            v-for="(item, idx) in MainManage"
            :key="idx"
            :index="item.path"
          >
            {{ item.name }}</el-menu-item
          >
        </el-sub-menu>

        <el-sub-menu index="3">
          <template #title>
            <el-icon><CreditCard /></el-icon>
            <span> 系统管理 SystemManage</span>
          </template>

          <el-menu-item index="/main/changePsd">
            • 修改密码 ChangePsd</el-menu-item
          >
        </el-sub-menu>
      </el-menu>
    </div>
  </div>
</template>

<script lang="ts" setup>
// 引入宿舍楼层路由
import { Dormitory } from "../../router/index";
// 引入综合管理路由
import { MainManage } from "../../router/index";
import { useRoute, onBeforeRouteUpdate } from "vue-router";

import {
  CreditCard,
  Document,
  House,
  Menu as IconMenu,
  Location,
  OfficeBuilding,
  Operation,
  Place,
  Setting,
} from "@element-plus/icons-vue";
import { onBeforeMount, ref } from "vue";

// 定义menu高亮
const active = ref("");
// 路由路径
const router = useRoute();
// 生命周期：挂载前会被调用1次
onBeforeMount(() => {
  // 将地址栏路径赋值给menu高亮
  active.value = router.fullPath;
});

// 监听路由路径变化  to：跳转到哪里  from：是从哪里跳转过来的
onBeforeRouteUpdate((to, from) => {
  // 跳转路由路径赋值给menu高亮
  active.value = to.fullPath;
});
</script>

<style lang="scss" scoped>
.el-menu {
  border-right: 0;
}

.main-box-menu-header {
  display: flex;
  justify-content: center;
  padding: 25px;
  img {
    width: 100%;
    height: 100%;
  }
}
.main-box-menu-list {
  width: 300px;
}
</style>
